Sitecore provides a facility for creating and running Scheduled Tasks.
The following steps describe how to configure such a task:
- Create your own class. This class must implement at least one public method with the following parameters:
using System;
using Sitecore.Data.Items;
using Sitecore.Tasks;
namespace CustomScheduleTasks
{
public class Task1
{
public void WriteToLogFile(Item[] itemArray, CommandItem commandItem, ScheduleItem scheduledItem)
{
Sitecore.Diagnostics.Log.Info("Test Scheduled task: "+DateTime.Now, this);
}
}
} - Compile this assembly and put the resulting file, for example: CustomScheduleTasks.dll, into the Sitecore web root /bin folder.
- In the Content Editor System area, locate the /system/Tasks/Commands item and create a new item based on the __Task Command master.
- Fill in the necessary fields:
Type: CustomScheduleTasks.Task1, CustomScheduleTasks
Method: WriteToLogFile
The Type indicates the namespace and assembly file. The Method indicates the method to call on the specified class.
Your command is now ready for use as a scheduled task.
- Locate the /system/Tasks/Schedules item and create new __Task Scheduler item
- Fill in the fields:
Command: select command created in the previous step.
Items: if you want to pass an array of items to your method you can place any XPath here. Items can be divided by pipe separator. For example: /Sitecore/content/Home/Globals/Item1 | /Sitecore/content/Home/Globals/Item2 -
Schedule: 20040720T235900|20060725T235900|127|01:00:00
In this example, the task will be executed from 20.07.2004 23:59:00 to 25.07.2006 23:59:00, every day (as specified by the number 127) and every 1 hour (as specified by 01:00:00).In the Schedule field you can place 4 parameters divided by pipe separators:
- From Date
- To Date
- Day of Weeks. Here DaysOfWeek enumeration is used. For Monday and Friday you should write 34 (2+32)
public enum DaysOfWeek
{
None = 0,
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64,
}
- Period of time e.g.: 01:00:00 – every 1 hour.
Note: the Auto Remove field is used when a scheduled item is executed. If AutoRemove is set, then the schedule item will be removed (deleted) after it is completed
- Set the necessary frequency and interval parameters in the web.config:
<scheduling>
<!-- Time between checking for scheduled tasks waiting to execute -->
<frequency>01:00:00</frequency>
<!-- Agent to process schedules embedded as items in a database -->
<agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="01:00:00">
<param desc="database">core</param>
<param desc="schedule root">/sitecore/system/tasks/schedules</param>
<LogActivity>true</LogActivity>
</agent>
...
Note: the interval for the DatabaseAgent should be set to more than the frequency parameter.